package com.hejia.bulbul.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.utils.DateTimeUtil;
import com.hejia.alauda.utils.Pager;
import com.hejia.alauda.utils.PagerHelper;
import com.hejia.bulbul.entity.PopularizeStatistic;
import com.hejia.bulbul.service.PopularizeStatisticService;
import com.hejia.bulbul.service.PopularizeVisitService;
import com.hejia.bulbul.service.UserOrderDayStatsService;

/**
 * 推广统计控制器
 * 
 * @author hyr
 *
 */
@Controller
@RequestMapping("popularizestatistic")
public class PopularizeStatisticController {
	
	private static final Log Logger = LogFactory.getLog(PopularizeStatisticController.class);
	
	@Resource
	private PopularizeVisitService popularizeVisitService;
	
	@Resource
	private PopularizeStatisticService popularizeStatisticService;
	
	@Resource
	private UserOrderDayStatsService userOrderDayStatsService;

	@RequestMapping("category/{biggerCategoryId}/{categoryId}/{startDate}/{endDate}.json")
	@ResponseBody
	public List<PopularizeStatistic> showPopularizeByCatogoryId(
			@PathVariable Long biggerCategoryId, @PathVariable Long categoryId,
			@PathVariable Date startDate, @PathVariable Date endDate) {

//		List<PopularizeStatistic> statistics = popularizeStatisticService.statisticPopularizeByCategoryIdAndDate(
//				biggerCategoryId,
//				categoryId,
//				startDate,
//				endDate);
		
		List<PopularizeStatistic> statistics = userOrderDayStatsService.statisticPopularizeByCategoryIdAndDate(
				biggerCategoryId,
				categoryId,
				startDate,
				endDate);
		
		return statistics;
	}

	@RequestMapping("biggercategory/{biggerCategoryId}/{startDate}/{endDate}.json")
	@ResponseBody
	public List<PopularizeStatistic> showPopularizeByBiggerCategoryId(
			@PathVariable Long biggerCategoryId, 
			@PathVariable Date startDate,
			@PathVariable Date endDate) {

		Logger.debug(String.format("showPopularizeByBiggerCategoryId biggerCategoryId %s startDate %s endDate %s", 
				biggerCategoryId, startDate, endDate));
		
//		List<PopularizeStatistic> statistics = popularizeStatisticService.statisticPopularizeByBiggerCategoryIdAndDate(
//				biggerCategoryId,
//				startDate,
//				endDate);
		
		return this.userOrderDayStatsService.statisticPopularizeByBiggerCategoryIdAndDate(
				biggerCategoryId,
				startDate,
				endDate);
	}

	@RequestMapping("popularizes")
	public String popularizes(
			SearchCategory searchCategory,
			Date startDate, 
			Date endDate, 
			Pager<PopularizeStatistic> pager,
			ModelMap model) {
		
		Date now = Calendar.getInstance().getTime();
		if (startDate == null) {
			startDate = DateTimeUtil.getDateDay(now);
		}else{
			startDate = DateTimeUtil.getDateDay(startDate);
		}
		if (endDate == null) {
			endDate = DateTimeUtil.getDateAndMaxTime(now);
		}else{
			endDate = DateTimeUtil.getDateAndMaxTime(endDate);
		}
		
//		Pager<PopularizeStatistic> popularizesStatistics = popularizeStatisticService.statisticPopularizes(
//				startDate,
//				endDate,
//				pager);
		Pager<PopularizeStatistic> popularizesStatistics = this.userOrderDayStatsService.statisticPopularizes(
				startDate,
				endDate,
				pager);
		
		Calendar yesterday = Calendar.getInstance();
		Calendar day7 = Calendar.getInstance();
		Calendar day30 = Calendar.getInstance();

		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		yesterday.add(Calendar.DAY_OF_MONTH, -1);
		day7.add(Calendar.DAY_OF_MONTH, -6);
		day30.add(Calendar.DAY_OF_MONTH, -29);

		PagerHelper<PopularizeStatistic> helper = new PagerHelper<>();
        helper.setPager(pager);
        Map<String, String> map = new HashMap<>();
        DateFormat secondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("startDate", secondFormat.format(startDate));
        map.put("endDate", secondFormat.format(endDate));
        if(searchCategory != null){
        	map.put("searchCategory", searchCategory.toString());
        }
        helper.setParams(map);
        helper.setBaseUrl("/popularizestatistic/popularizes");
        
        model.put("pagerHelper", helper);
		model.put("searchCategory", searchCategory);
		model.put("today", format.format(now));
		model.put("yesterday", format.format(yesterday.getTime()));
		model.put("day7", format.format(day7.getTime()));
		model.put("day30", format.format(day30.getTime()));
		model.put("startDate", startDate);
		model.put("endDate", endDate);
		model.put("statistics", popularizesStatistics);
		
		return "popularizestatistic/popularizes";
	}

	/**
	 * 查询类别
	 * 
	 * @author hyr
	 *
	 */
	public enum SearchCategory {

		/**
		 * 今天
		 */
		TODAY,

		/**
		 * 昨天
		 */
		YESTODAY,

		/**
		 * 最近7天
		 */
		LATEST7DAY,

		/**
		 * 最近30天
		 */
		LATEST30DAY,
		
		/**
		 * 日期
		 */
		DATE,
		
		/**
		 * 全部
		 */
		ALL
	}
	
	@InitBinder
    //此处的参数也可以是ServletRequestDataBinder类型
    public void initBinder(WebDataBinder binder) throws Exception {
        //注册自定义的属性编辑器
        //1、日期
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor dateEditor = new CustomDateEditor(df, true);
        //表示如果命令对象有Date类型的属性，将使用该属性编辑器进行类型转换
        binder.registerCustomEditor(Date.class, dateEditor);

    }
}
